{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "1. 基于iris数据 建立KNN模型实现数据分类\n",
    "+ 对数据进行标准环处理,选择一个维度可视化处理后效果\n",
    "+ 进行与元数据等维度PCA,查看各主成分的方差比例\n",
    "+ 保留合适的主成分,可视化降维后的数据\n",
    "+ 基于降维后数据建立KNN模型,与元数据表现进行对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   sepal length  sepal width  petal length  petal width       target  label\n",
      "0           5.1          3.5           1.4          0.2  Iris-setosa      0\n",
      "1           4.9          3.0           1.4          0.2  Iris-setosa      0\n",
      "2           4.7          3.2           1.3          0.2  Iris-setosa      0\n",
      "3           4.6          3.1           1.5          0.2  Iris-setosa      0\n",
      "4           5.0          3.6           1.4          0.2  Iris-setosa      0\n",
      "(150, 4) (150,)\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "data = pd.read_csv('iris_data.csv')\n",
    "print(data.head())\n",
    "X = data.drop(['target','label'],axis=1)\n",
    "y = data.loc[:,'label']\n",
    "print(X.shape,y.shape)#打印X，y的维度\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.96\n"
     ]
    }
   ],
   "source": [
    "#建立模型-预测-计算准确率\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "KNN = KNeighborsClassifier(n_neighbors=3)\n",
    "KNN.fit(X,y)\n",
    "y_predict = KNN.predict(X)\n",
    "from sklearn.metrics import accuracy_score\n",
    "accuracy = accuracy_score(y,y_predict)\n",
    "print(accuracy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "#数据标准化处理\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "X_norm = StandardScaler().fit_transform(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.843333333333335 -4.736951571734001e-16 0.8280661279778629 1.0\n"
     ]
    }
   ],
   "source": [
    "X1_mean = X.loc[:,'sepal length'].mean()\n",
    "X1_norm_mean = X_norm[:,0].mean()\n",
    "X1_sigma = X.loc[:,'sepal length'].std()\n",
    "X1_norm_sigma = X_norm[:,0].std()\n",
    "print(X1_mean,X1_norm_mean,X1_sigma,X1_norm_sigma)\n",
    "#均值约等于0和标准差为1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAlAAAAEvCAYAAACKfv/MAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAASB0lEQVR4nO3df6yleV0f8Pene6GyiFmanVpkmV5IzKbUxLCZEHATQlio6BC2TTRZUijSmqmNUrBNdLRJ+XeMxthfsZkASuO6pl2hEgYtVCWmSdl0d1lll9GKMMLK4q6agmIT3PDpH/fMOr3emXu/5zzPOffMvF7JzTznnOec7+d7n3M+877Pc85zqrsDAMDR/bVNFwAAsG0EKACAQQIUAMAgAQoAYJAABQAwSIACABi0s87Bbr311t7d3V3nkMCGPfTQQ3/U3Sc2Xceq9C+48Vyr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jD/z/D+X5BVVdfNijLuy956FK61j/sdGVb0+yQ8neWN3//mm6+EZvqpnCyz6yHuSXOzun9x0PSNWOhP5cVJV35ck3f0fk3xXkn9WVU8n+b9J7pnwr8JvTPKBxf9PO0l+vrt/Zd/4H87eJ5E+neTPk7xtorGPOv6c80+Stye5d9GUPpPkbWuc/1HGn3X+VXVzktcl+adXXLe2+R9h/Nnm390PVNX92TtM+HSSTyQ5v+btf9z8+yR/PclHF6/Lj3f39222pNVU1T/I3uHfE0kuVNUj3f3tGy5rSF+HX9VTVfdl71O2t1bV40ne1d3v2WxVK7szyVuSfLKqHllc96OLvefHmjORAwAM2rpDeAAAmyZAAQAMEqAAAAYJUAAAgwQoAIBBAhQAwCABCgBgkAAFADDo/wGklnbz5GfxSgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 720x360 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib import pyplot as plt\n",
    "fig1 = plt.figure(figsize=(10,5))\n",
    "plt.subplot(121)\n",
    "plt.hist(X.loc[:,'sepal length'],bins=100)#X有四个维度 取一个\n",
    "plt.subplot(122)\n",
    "plt.hist(X_norm[:,0],bins=100)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(150, 4)\n"
     ]
    }
   ],
   "source": [
    "print(X.shape)\n",
    "#4维的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.72770452 0.23030523 0.03683832 0.00515193]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.decomposition import PCA \n",
    "pca = PCA(n_components=4)\n",
    "X_pca = pca.fit_transform(X_norm)\n",
    "\n",
    "var_ratio = pca.explained_variance_ratio_#计算比例\n",
    "print(var_ratio)\n",
    "#主成分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAPLElEQVR4nO3dbYydaV3H8e+PWUtAg6A7grZd2kAJFrJsYCiiIhCzsctqykYiXQgrKDaVVOMLCNUoMeENm9WEGIqTBhuiL2xEnibsQE0wPETAzKwpC10sGSrSoZgdQCALG0uXvy/mQA7TM3PumZ7pmbn2+0kme66Huc+/13Z+uXL3XPekqpAkbX+PGXcBkqTRMNAlqREGuiQ1wkCXpEYY6JLUiBvG9cY33nhj7dmzZ1xvL0nb0n333ff1qpocNDa2QN+zZw/z8/PjentJ2paS/PdqY95ykaRGGOiS1AgDXZIaYaBLUiMMdElqhIEuSY0w0CWpEQa6JDXCQJekRoztpOi12HP83nGXMFZfftvt4y5B0hbkDl2SGmGgS1IjDHRJakSnQE9yMMn5JAtJjg8Yf1OSs72vzyd5JMnPjL5cSdJqhgZ6kgngBHAbsB+4M8n+/jlVdU9V3VJVtwB/Cny8qr65GQVLkgbrskM/ACxU1YWqugycBg6tMf9O4B9HUZwkqbsugb4TuNjXXuz1XSXJ44GDwHuvvTRJ0np0CfQM6KtV5v4W8G+r3W5JciTJfJL5paWlrjVKkjroEuiLwO6+9i7g0ipzD7PG7ZaqOllVU1U1NTk58FfiSZI2qEugzwH7kuxNsoPl0J5ZOSnJTwMvBj442hIlSV0MPfpfVVeSHAPOABPA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Js1yeZbpZV16+qXl1VN1XVHuCNLP893HZhDgZ6Z7V8pPYO4Nbex57OAX9J7yFkST4JvAf49SSLSX5jbMVuQUPW73eAXwNe2/u42dkkt6x+tUefIev3i8C/J/ksy5/S+Kuq+tzYit2Chv38tsKj/5LUCHfoktQIA12SGmGgS1IjDHRJaoSBLkmNMNAlqREGuiQ14v8BlS1wR5xS03wAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig2 = plt.figure()\n",
    "plt.bar([1,2,3,4],var_ratio)\n",
    "plt.xticks([1,2,3,4],['PC1','PC2','PC3','PC4'])\n",
    "plt.ylabel(\"variance ratio \")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "可以看出前两占比高 所以可以排除后两 做成2维的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "from sklearn.decomposition import PCA \n",
    "pca = PCA(n_components=2)\n",
    "X_pca = pca.fit_transform(X_norm)\n",
    "print(type(X_pca))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAD4CAYAAADxeG0DAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgA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SAeAPao6IiLfBwBVXRPxsFwTkc8AGAPw3wHcoap7Ix6SIyKSAfAbAH8KoAfArwB8WVV/HenAPBCRywB8BOBHqroo6vF4JSLNAJpVdb+InA1gH4CVSfv/REQEQL2qfiQiWQAvAehU1VcjHlp8ZuTFID6uHkA8PmFcUtWdqjoy/uWrABJ59piqvqGqR6IehwfLALypqkdV9TSAvwdwTcRj8kRVXwDwftTj8EtV+1R1//i//xuANwDMjnZU7mnBR+NfZsf/iUWcik0gBwARuV9E3gZwA4BqOLPrZgD/FPUgUmY2gLdLvu5BAoNGtRKReQCWAPhltCPxRkQyIvIagN8D+LmqxuJ9VDSQi8guEXnd4J9rAEBVv6uqcwA8A+C/VHJsbti9j/HXfBfACArvJZacvI8EEoNrsZg1pZ2InAVgK4C/LPsNPDFUdVRVL0LhN+1lIhKLJa+KHvWmqp93+NKfAMgDuDvE4Xhm9z5E5CYAVwG4QuOyCWHAxf8fSdIDYE7J1y0AjkU0Fho3vqa8FcAzqrot6vH4par9IvK/AFwJIPLN6NgsrYjIhSVf/jmAw1GNxQ8RuRLAGgB/rqqnoh5PCv0KwIUiMl9EzgDwFwD+IeIxpdr4JuHjAN5Q1YeiHo9XIjKzmIUmInUAPo+YxKk4Za1sBbAQhUyJ3wK4VVV7ox2VeyLyJoAzAbw3funVhGbffBHA3wCYCaAfwGuquiLaUTkjIl8A8NcAMgCeUNX7Ix6SJyLydwD+BIW2qe8AuFtVH490UB6IyB8DeBHAIRT++waA76jqP0Y3KvdEpA3A0yj8vaoBsEVV10c7qoLYBHIiIvImNksrRETkDQM5EVHCMZATESUcAzkRUcIxkBMRJRwDORFRwjGQExEl3P8H8tRZb8F3eeYAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig3 = plt.figure()\n",
    "s1= plt.scatter(X_pca[:,0][y==0],X_pca[:,1][y==0])\n",
    "s2 = plt.scatter(X_pca[:,0][y==1],X_pca[:,1][y==1])\n",
    "s3 = plt.scatter(X_pca[:,0][y==2],X_pca[:,1][y==2])\n",
    "plt.legend((s1,s2,s3),('s1','s2','s3'))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9466666666666667\n"
     ]
    }
   ],
   "source": [
    "#准确率\n",
    "# from sklearn.neighbors import KNeighborsClassifier\n",
    "KNN = KNeighborsClassifier(n_neighbors=3)\n",
    "KNN.fit(X_pca,y)\n",
    "y_predict_pca = KNN.predict(X_pca)\n",
    "from sklearn.metrics import accuracy_score\n",
    "accuracy = accuracy_score(y,y_predict_pca)\n",
    "print(accuracy)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 维度下降了 信息仍然保留了\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PyCharm (Learning)",
   "language": "python",
   "name": "pycharm-bd849f12"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
